<?php defined('SYSPATH') or die('No direct script access.');
/**
* Module Dashboard
*/
class Module_perbaikan_perbaikan {
	
	private $controller;
	private $ss;
	private $message;
	private $thesystems;
	private $mydatetime;
	private $arraylist;
	
	static function &factory() {
	   $o = new Module_perbaikan_perbaikan();
	  return $o;	
	}
	
	public function __construct()
	{
		$this->controller	= Request::current()->controller();
		$this->ss					= Session::instance();
		$this->thesystems	= Helper_theSystems::factory();
		$this->mydatetime	= Helper_MyDateTime::factory();
		$this->message		= Language_Message::factory();
		$this->arraylist	= Helper_ArrayList::factory();
	}
	
	public function perbaikan($id, $command)
	{
 		
		$value_rpp		= Arr::get($_REQUEST,'rpp');
		$rpp			= (!empty($value_rpp) ? $value_rpp : '5');
		$halaman		= Arr::get($_GET,'page',1);
		$offset			= ($halaman - 1)*$rpp;
		
		switch ($command)
		{
		
		case "create":
				
					  
					// Create Data
					$id                       = $_POST['id'];
					$tgl                      = $_POST['tgl'];
					$id_spkp                  = $_POST['id_spkp'];
					$id_truk                  = $_POST['id_truk'];
					$total_harga              = $_POST['total_harga'];
					$keterangan_kerusakan     = $_POST['keterangan_kerusakan'];
					$jml_pemakaian_sukucadang = $_POST['jml_pemakaian_sukucadang'];
				  
				  $query = DB::insert('perbaikan', array('id','tgl','id_spkp','id_truk','total_harga','keterangan_kerusakan','jml_pemakaian_sukucadang'))
				  ->values(array($id,$tgl,$id_spkp,$id_truk,$total_harga,$keterangan_kerusakan,$jml_pemakaian_sukucadang));
				  $result = $query->execute();
					  if ($result[1] == 1) {
						$this->message->store_message('c_success');
						$r = Request::current();
						$r->redirect($this->controller.'/perbaikan?id='.$result[0]);
					  } else {
						$this->message->store_message('c_fails');
					  }
				  break;
		
		case "update":
				 
					// Update Data
					$p['id']                       = $_POST['id'];
					$p['tgl']                      = $_POST['tgl'];
					$p['id_spkp']                  = $_POST['id_spkp'];
					$p['id_truk']                  = $_POST['id_truk'];
					$p['total_harga']              = $_POST['total_harga'];
					$p['keterangan_kerusakan']     = $_POST['keterangan_kerusakan'];
					$p['jml_pemakaian_sukucadang'] = $_POST['jml_pemakaian_sukucadang'];
				  
				  $query = DB::update('perbaikan')
					  ->set($p)
					  ->where('id', '=', $id)
					  ->limit(1);
				  $result = $query->execute();
					  if ($result == 1) {
						$this->message->store_message('u_success');
						$r = Request::current();
						$r->redirect($this->controller.'/perbaikan?id='.$id);
					  } else {
						$this->message->store_message('u_fails');
					  }
				  break;
		
		case "delete":
				
				  $query = DB::delete('perbaikan')
					  ->where('id', '=', $id)
					  ->limit(1);
				  $result = $query->execute();
					  if ($result == 1) {
						$this->message->store_message('d_success');
						$r = Request::current();
						$r->redirect($this->controller.'/perbaikan');
					  } else {
						$this->message->store_message('d_fails');
					  }
				  break;
		
		}
		
		/*pencarian start*/
		$key   = Arr::get($_REQUEST,'key');
		$value = Arr::get($_REQUEST,'value');
		/*pencarian end*/

		$all_items = DB::select()
			->from('perbaikan');

		/*pencarian start*/
		if(!empty($key))
		{
			if(!empty($value)) {
				$all_items = $all_items->where($key, 'LIKE', '%'.$value.'%');
			}
		}
		/*pencarian end*/

		$all_items = $all_items->execute();
		$count = $all_items->count();
		
		$perbaikanlists = DB::select()
			->from('perbaikan')
			->limit($rpp)
			->offset($offset);

		/*pencarian start*/
		if(!empty($key))
		{
			if(!empty($value)) {
				$perbaikanlists = $perbaikanlists->where($key, 'LIKE', '%'.$value.'%');
			}
		}
		/*pencarian end*/
			
		$perbaikanlists = $perbaikanlists->execute();
		
		$perbaikan_update = DB::select()
			->from('perbaikan')
			->where('id', '=', $id)
			->limit(1)
			->execute();
		
		$pagination = Helper_myFunctions::factory()->paginations($count,$rpp);
		
		$content['title']		= 'User Administrator';
		$content['content']	= View::factory('inc/perbaikan')
			->set('perbaikanlists',$perbaikanlists)
			->set('perbaikan_update',$perbaikan_update[0])
			->set('pagination',$pagination)
			->render();
		
		return $content;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}